<?php
namespace  app\admin\controller;

use think\Db;
use app\common\model\Company as CompanyModel;
use app\common\model\Address as AddressModel;
use app\admin\tool\Parser;
use think\Loader;
use think\Request;

class Company extends Base{

    protected $company_type = [];

    /**
     * 更新B端和城市运营商的数据
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function update(){

        $this->data['title'] = '更新B端 城市运营商数据';

        $this->data['model'] = Db::table('company')->order('update_time desc')->find();


        return view("",$this->data);
    }



    /**
     * 公司 工厂 B端 城市运营商 列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index(){

        $where = ['is_delete'=>0];
        $this->data['title']  = '公司列表';
        $this->company_type = get_company_type();
        $this->data['column'] = $this->column();
        $this->data['model']  = CompanyModel::where($where)->paginate(10);
        $this->data['parser'] = new Parser();

        return view('',$this->data);
    }


    /**
     * 回收站
     */
    public function recycle(){
        $where = ['is_delete'=>1];
        $this->data['title']  = '已删除公司列表';
        $this->company_type = get_company_type();
        $this->data['column'] = $this->column();
        $this->data['model']  = CompanyModel::where($where)->paginate(10);
        $this->data['parser'] = new Parser();

        return view('',$this->data);
    }



    /**
     * 只能添加工厂或者世博园
     */
    public function add(){

        $this->data['title'] = '添加工厂或者展览会馆';

        return view('',$this->data);
    }


    /**
     * 只支持工厂 和 世博园信息的编辑  B端和 城市运营商的数据从平台接口获取
     * @param Request $request
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function edit(Request $request){
        $id = $request->param('id');
        $company = CompanyModel::get($id);
        if(!in_array($company['type'],[2,3])){
            $this->error("B端和城市运营商不能编辑");
        }
        $this->data['model'] = $company;
        $this->data['title'] =  "修改世博园或者展览会馆";

        return view('',$this->data);
    }


    /**
     * @param Request $request
     * @throws
     */
    public function handle(Request $request){
        $data = $request->post();
        $validate = Loader::validate("company");
        if(!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $data['password'] = create_password($data['password']);
        $data['update_time'] = time();

        if(empty($data['id'])){
            $res = CompanyModel::where("mobile",$data['mobile'])->count();
            if($res){
                $this->error('该手机号码已经存在');
            }
            $data['add_time'] = $data['update_time'];
            if(CompanyModel::create($data)){
                $this->success("新增成功");
            }
        }else{
            $companyModel = new CompanyModel();
            if($companyModel->save($data,['id'=>$data['id']])){
                $this->success("更新成功");
            }
        }

        $this->error("操作失败");
    }



    /**
     * 不能真正删除数据 修改is_delete 字段值
     * @param Request $request
     * @throws
     */
    public function delete(Request $request){
        $id = $request->param('id');

        $company = CompanyModel::get($id);
        if(!$company){
            $this->error("参数错误");
        }

        if(!in_array($company['type'],[2,3])){
            $this->error("B端和城市运营商不能删除");
        }

        $company->is_delete = 1;
        $company->save();

        $this->error('删除成功');

    }


    /**
     * 恢复
     * @param Request $request
     * @throws
     */
    public function recover(Request $request){
        $id = $request->param('id');

        $company = CompanyModel::get($id);
        if(!$company){
            $this->error("参数错误");
        }

        if(!in_array($company['type'],[2,3])){
            $this->error("B端和城市运营商不支持该操作");
        }

        $company->is_delete = 0;
        $company->save();

        $this->error('恢复成功');

    }


    public function column(){
        return [
            'company_name' => ['name'=>'公司名称','fun'=>'text','options'=>''],
            'type'         => ['name'=>'类型',    'fun'=>'translateMulti', 'options'=>['map'=>$this->company_type]]
        ];
    }

}